package commands.core
{
	import cultu.core.message.Role;
	import cultu.core.message.RoleCriteria;
	import cultu.core.message.RoleList;
	import cultu.core.message.RoleScenario;
	import cultu.core.service.RoleManager;
	
	import flash.utils.Dictionary;
	
	import model.Model;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import wizeoo.commands.WIZRemoteCall;
	import wizeoo.utils.WIZCollectionUtils;
	
	public class FindRolesCmd extends WIZRemoteCall
	{
		/**Référence au model*/
		private var mod:Model = Model.getInstance();
		
		public function FindRolesCmd(pCallback:Function=null)
		{
			super(pCallback);
		}
		
		override public function wizExecute():void
		{
			var rm:RoleManager = new RoleManager();
			var rc:RoleCriteria = new RoleCriteria();
			var rs:RoleScenario = new RoleScenario();
			
			rm.addEventListener(ResultEvent.RESULT,result,false,0,true);
			rm.addEventListener(FaultEvent.FAULT,fault,false,0,true);
			
			rc.jpqlQuery = 'SELECT r FROM Role r';
			
			rm.findRoles(null,rs,rc);
		}
		
		override protected function wizResult(data:Object):void
		{
			mod.roleCollection = new ArrayCollection();
			mod.roleDic = new Dictionary(true);
			
			if(data && data.result)
			{
				var myColl:ArrayCollection = (data.result as RoleList).role;
				mod.roleCollection.addAll(myColl);
				mod.roleDic = WIZCollectionUtils.arrayCollToDic(mod.roleCollection,'code');
			}
		}
		
		override protected function wizFault(info:Object):void
		{
			trace('unable to find roles');	
		}
	}
}